package ${controllerUrl};

import ${entityUrl}.${entityName};
import ${entityUrl}.${userClassName};
import ${entityDtoUrl}.${entityName}Dto;
import ${entityDtoUrl}.${userClassName}Dto;
import ${serviceUrl}.${entityName}Service;
import ${utilsUrl}.enums.DataStatusEnum;
import com.lyhealth.exception.MsgException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitee.flying.cattle.mdg.aid.JsonResult;
import com.gitee.flying.cattle.mdg.aid.PageParam;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.Resource;
/**
 *
 * ${entityComment}Controller接口层
 * @author ${author}
 *
 */
@RestController
public class ${entityName}Controller extends BaseController{

    protected JsonResult jsonResult = new JsonResult();

    @Resource
    private ${entityName}Service ${objectName}Service;

    /**
    * 通过主健id查询对象
    * @param id
    * @return JsonResult
    */
    @GetMapping("/${objectName}/getById")
    public JsonResult getById(@RequestParam(value = "id", required = true) Long id){
        ${entityName} ${objectName} = ${objectName}Service.getById(id);

        if(${objectName}==null){
            throw new MsgException("对象不存在");
        }

        ${entityName}Dto ${objectName}Dto = new ${entityName}Dto();
        BeanUtils.copyProperties(${objectName}, ${objectName}Dto);
        return jsonResult.success(${objectName}Dto);
    }

    /**
    * 通过主健id删除对象
    * @param ${objectName}Dto
    * @return JsonResult
    */
    @DeleteMapping("/${objectName}/removeById")
    public JsonResult removeById(@RequestBody ${entityName}Dto ${objectName}Dto){
        ${entityName} ${objectName} = ${objectName}Service.getById(${objectName}Dto.getId());

        if(${objectName}==null){
            throw new MsgException("对象不存在!");
        }

        ${objectName}Dto.setDataStatus(DataStatusEnum.DELETE.getCode());
        ${objectName}Service.updateById(${objectName}Dto);

        return jsonResult.success("删除成功");
    }

    /**
    * 新增对象
    * @param ${objectName}Dto
    * @return JsonResult
    */
    @PutMapping("/${objectName}/save")
    public JsonResult save(@RequestBody ${entityName}Dto ${objectName}Dto){
        ${objectName}Dto.setCreateId(getLyUserDto().getId());
        ${objectName}Service.save(${objectName}Dto);
        return jsonResult.success("新增成功");
    }

    /**
    * 更新对象
    * @param ${objectName}Dto
    * @return JsonResult
    */
    @PatchMapping("/${objectName}/updateById")
    public JsonResult updateById(@RequestBody ${entityName}Dto ${objectName}Dto){
        ${objectName}Service.updateById(${objectName}Dto);
        return jsonResult.success("更新成功");
    }

    /**
    * 分页查询
    * @param param
    * @return PageInfo<${entityName}Dto>
    */
    @PostMapping("/${objectName}/pageList")
    public JsonResult<IPage> pageList(@RequestBody PageParam<${entityName}Dto> param){
        Page<${entityName}> page = new Page<${entityName}>(param.getPageNum(),param.getPageSize());
        IPage<${entityName}Dto> iPage = ${objectName}Service.pageSearch(page, param.getParam());

        Long count = 0L;
        if(iPage.getRecords()!=null&&iPage.getRecords().size()>0){
            count = ${objectName}Service.countSearch(param.getParam());
        }

        iPage.setTotal(count);
        return jsonResult.success(iPage);
    }

    /**
    * 查询列表
    * @param ${objectName}Dto
    * @return List<${entityName}Dto>
    */
    @PostMapping("/${objectName}/list")
    public JsonResult<List<${entityName}Dto>> list(@RequestBody ${entityName}Dto ${objectName}Dto){
        List<${entityName}Dto> ${objectName}Dtos = new ArrayList<${entityName}Dto>();
        ${objectName}Dtos = ${objectName}Service.listSearch(${objectName}Dto);
        return jsonResult.success(${objectName}Dtos);
    }

    /**
    * 条件查询记录数量
    * @param ${objectName}DtoSearch
    * @return List<${entityName}Dto>
    */
    @PostMapping("/${objectName}/count")
    public JsonResult<Long> count(@RequestBody ${entityName}Dto ${objectName}DtoSearch){
        Long count = ${objectName}Service.countSearch(${objectName}DtoSearch);

        return jsonResult.success(count);
    }
}